package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.CheckGroupDto;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupItemService;
import com.itheima.service.CheckGroupService;
import com.itheima.support.PageResult;
import com.itheima.support.QueryPageBean;
import com.itheima.vo.CheckGroupVO;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@DubboService
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    //调用CheckGroupItemService中的方法 IOC 注入
    @Resource
    private CheckGroupItemService checkGroupItemService;

    @Override
    @Transactional
    public boolean saveGroup(CheckGroupVO checkGroupVO) {
        //保存检查组的基本信息
        this.saveOrUpdate(checkGroupVO);
        //调用中间表删除中间表
        checkGroupItemService.deleteByGroupId(checkGroupVO.getId());
        //新增中间表的信息
        checkGroupItemService.saveCheckGroupItem(checkGroupVO.getId(),checkGroupVO.getItems());
        return true;
    }

    //TODO : 分页
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        //where (code like '%001%' or name like '%001%' or help_code like '%001%')
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        if(ObjectUtils.isEmpty(queryPageBean.getQueryString()) == false){
            queryWrapper.like("code",queryPageBean.getQueryString()).or()
                    .like("name",queryPageBean.getQueryString()).or()
                    .like("help_code",queryPageBean.getQueryString());
        }
        IPage<CheckGroup>page=this.page(new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()),queryWrapper);
        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public List<CheckGroupDto> selectBySetmealId(Long setmealId) {
        return baseMapper.selectBySetmealId(setmealId);
    }


}
